package com.webserver.controller;

import com.webserver.annotations.Controller;
import com.webserver.annotations.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.util.DBUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@Controller
public class UserController {
    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理注册信息");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        if (username == null || username.isEmpty() ||
        password ==null || password.isEmpty() ||
        nickname ==null || nickname.isEmpty() ||
        ageStr == null || ageStr.isEmpty() || !ageStr.matches("[0-9]+")
        ){
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        System.out.println(username+","+password+","+nickname+","+ageStr);
        int age = Integer.parseInt(ageStr);
        try (
                Connection connection = DBUtil.getConnetion();
                ){
            Statement statement = connection.createStatement();
            String sql = "SELECT 1 FROM userinfo WHERE username='"+username+"'";
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()){
                response.sendRedirect("/have_user.html");
                return;
            }
            sql = "INSERT INTO userinfo (username,password,nickname,age) " +
                    "VALUES('"+username+"','"+password+"','"+nickname+"',"+age+")";
            int num = statement.executeUpdate(sql);
            if (num>0){
                response.sendRedirect("/reg_success.html");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/loginUser")
    public void login (HttpServletRequest request,HttpServletResponse response){
        System.out.println("开始处理登录");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username+" "+password);
        if (username ==null || username.isEmpty() ||
        password==null || password.isEmpty()){
            response.sendRedirect("login_info_error.html");
            return;
        }
        try (
                Connection connection = DBUtil.getConnetion();
                ){
            Statement statement = connection.createStatement();
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM userinfo " +
                    "WHERE username='"+username+"' " +
                    "AND password='"+password+"'";
            ResultSet rs = statement.executeQuery(sql);
            if (rs.next()){
                response.sendRedirect("/login_success.html");
            }else {
                response.sendRedirect("/login_fail.html");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
